package com.yjc.drygoods.search.controller;

import com.yjc.common.exception.BizCodeEnume;
import com.yjc.common.to.PurchaseDetailTo;
import com.yjc.common.to.SkuEsModel;
import com.yjc.common.to.SkuInfoTo;
import com.yjc.common.utils.R;
import com.yjc.drygoods.search.servcie.ElasticSearchServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author yjc
 * @create 2022-09-07 15:53
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSearchSave {

    @Autowired
    ElasticSearchServcie elasticSearchServcie;

    @PostMapping("/update/skuPrice")
    public R updateSkuPrice(@RequestBody List<SkuInfoTo> skuInfos) throws IOException {
        elasticSearchServcie.updateSkuPrice(skuInfos);
        return R.ok();
    }

    /**
     * 保存上一年的所有采购需求
     *
     * @return
     */
    @PostMapping("/oldYear/purchaseDetail")
    public R saveOldYearPurchaseDetail(@RequestBody List<PurchaseDetailTo> purchaseDetailTos) throws IOException {
        elasticSearchServcie.saveOldYearPurchaseDetail(purchaseDetailTos);
        return R.ok();

    }


    /**
     * 更新销量(ES)
     *
     * @param skuInfos
     * @return
     * @throws IOException
     */
    @PostMapping("/update/saleCount")
    public R updateEsSaleCount(@RequestBody List<SkuInfoTo> skuInfos) throws IOException {

        elasticSearchServcie.updateEsSaleCount(skuInfos);
        return R.ok();

    }

    /**
     * 商品上架
     *
     * @param skuEsModels
     * @return
     */
    @PostMapping("/up/product")
    R saveSkuEsModels(@RequestBody List<SkuEsModel> skuEsModels) {
        Boolean b = false;
        try {
            b = elasticSearchServcie.saveSkuEsModels(skuEsModels);
        } catch (IOException e) {
            log.info("商品上架异常：{}", e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCodeException(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessageException());
        }
        if (!b) {
            return R.ok();
        } else {
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCodeException(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMessageException());
        }
    }

    /**
     * 干货下架
     *
     * @param skuIds
     * @return
     */
    @PostMapping("/down/all/sku")
    R downAllSku(@RequestBody List<Long> skuIds) throws IOException {
        elasticSearchServcie.downAllSku(skuIds);
        return R.ok();
    }
}
